/**
 * Created by zoushuiyun on 2016/12/26.
 */
import { Pipe, PipeTransform } from '@angular/core';
import { isNullOrUndefined } from 'util';
import { USER_ROLE_OPTION, SEX_OPTION, USER_STATUS_OPTION, YESNO_OPTION } from '../../constant/dictionary';
import { myValidNull } from '@core/util/string-utils';
import { QCLOUD_DOMAIN } from '../../constant/config';
import { formLongDate } from '@core/util/date-utils';
// import {QINIU_DOMAIN} from "../../constant/config";


/**
 * 性别，从 dictionary 查找
 * one to one
 */
@Pipe({ name: 'sexPipe' })
export class SexPipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    let result = '';
    const sexDicts: any[] = SEX_OPTION;
    for (const sex of sexDicts) {
      if (sex.key === value) {
        result = sex.value;
        break;
      }
    }
    return result;
  }
}

@Pipe({ name: 'longDatePipe' })
export class LongDatePipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    return formLongDate(value);
  }
}

/**
 * 图+前缀
 */
@Pipe({ name: 'qCloudDomain' })
export class QCloudDomain implements PipeTransform {
  constructor() {
  }

  transform(value: any): string {
    let result = QCLOUD_DOMAIN+value;
    if(value.startsWith(QCLOUD_DOMAIN)){
      result = value
    }
    return result;
  }
}

/**
 * 1是0否状态，从 dictionary 查找
 * one to one
 */
@Pipe({ name: 'yesNoPipe' })
export class YesNoPipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    let result = '';
    const yesNoDicts: any[] = YESNO_OPTION;
    for (const o of yesNoDicts) {
      if (o.key === value) {
        result = o.value;
        break;
      }
    }
    return result;
  }
}

/**
 * 转换 json key 显示
 */
@Pipe({name: 'keysPipe'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push(key);
    }
    return keys;
  }
}

/**
 * 用户状态，从 dictionary 查找
 * one to one
 */
@Pipe({ name: 'userStatusPipe' })
export class UserStatusPipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    let result = '';
    const userDicts: any[] = USER_STATUS_OPTION;
    for (const user of userDicts) {
      if (user.key === value) {
        result = user.value;
        break;
      }
    }
    return result;
  }
}

/**
 * 用户类型，从 dictionary 查找
 * one to one
 */
@Pipe({ name: 'userRolePipe' })
export class UserRolePipe implements PipeTransform {
  constructor() {
  }

  transform(value: any): string {
    let result = '';
    const groupDicts: any[] = USER_ROLE_OPTION;
    if(value === "ROLE_ADMIN"){
      return "超级管理员";
    }
    for (const user of groupDicts) {
      if (user.key === value) {
        result = user.value;
        break;
      }
    }
    return result;
  }
}

/**
 *  后台获取的字典 翻译器
 *
 *
 *  one to one
 */
@Pipe({ name: 'dicsNamePipe' })
export class DicsNamePipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, groupDicts: any[], key?: any, desc?: any): string {
    let keyName: any = 'key';
    let descName: any = 'value';
    if (!myValidNull(key)) {
      keyName = key;
    }
    if (!myValidNull(desc)) {
      descName = desc;
    }
    let result = value;
    for (const obj of groupDicts) {
      if (obj[keyName] === value) {
        result = obj[descName];
        break;
      }
    }
    return result;
  }
}


/**
 * 字符串截取
 */
@Pipe({ name: 'stringCutPipe' })
export class StringCutPipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, args: number): string {
    if (isNullOrUndefined(value)) {
      return '';
    }
    if (value.length <= args) {
      return value;
    } else {
      return value.substring(0, args);
    }

  }
}

/**
 * 字符串截取 带。。。
 */
@Pipe({ name: 'stringEllipsisCutPipe' })
export class StringEllipsisCutPipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, args: number): string {
    if (isNullOrUndefined(value)) {
      return '';
    }
    if (value.length <= args) {
      return value;
    } else {
      return value.substring(0, args) + '...';
    }

  }
}

@Pipe({ name: 'filterPipe', pure: false })
export class FilterPipe {
  transform(array: Array<any>, filterName: string, filterValue: any): Array<any> {
    const result: Array<any> = new Array;
    array.forEach((arr) => {
      if (arr[filterName] === filterValue) {
        result.push(arr);
      }
    });
    return result;
  }
}

/**
 * 将arr中selected为true的元素排在前面 ---可自行扩展
 */
@Pipe({ name: 'selectedPipe', pure: false })
export class SelectedPipe {
  transform(array: Array<any>, args: string): Array<any> {
    const result: Array<any> = new Array;
    array.forEach((arr) => {
      if (arr.selected) {
        result.push(arr);
      }
    });
    array.forEach((arr) => {
      if (!arr.selected) {
        result.push(arr);
      }
    });
    return result;
  }
}
